<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.personName" placeholder="姓名" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
    >
      <el-table-column label="编号" align="center" fixed width="150px">
        <template slot-scope="{row}">
          <span>{{ row.personId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.personName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center">
        <template slot-scope="{row}">
          <el-image style="max-width: 50px; max-height: 28px" :src="row.personImg" fit="contain" :preview-src-list="[row.personImg]" />
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center">
        <template slot-scope="{row}">
          <span>{{ row.personDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序权重" align="center">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="plain" type="primary"> {{ row.personRank }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="详情介绍" align="center">
        <template slot-scope="{row}">
          <el-popover placement="left" width="600" trigger="click">
            <div style="margin: 10px;">
              <div v-if="row.personContent" class="issue-content" v-html="row.personContent" />
            </div>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="最近修改时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button type="primary" size="mini" @click="handleRank(row)">
            排序
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.personId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showExcelPersonList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogVisible" width="800px" top="20px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 100%">
        <el-form-item label="姓名" prop="personName" style="width: 90%">
          <el-input v-model="temp.personName" />
        </el-form-item>
        <el-form-item label="照片" prop="personImg" style="width: 90%">
          <el-input v-model="temp.personImg" placeholder="图片链接" />
          <img-upload-single :key="imgUploadKey" ref="imgUpload" @onUpload="uploadImg" @onRemove="removeImg"></img-upload-single>
        </el-form-item>
        <el-form-item label="所属单位" prop="personDesc" style="width: 90%">
          <el-input v-model="temp.personDesc" type="textarea" :row="2" />
        </el-form-item>
        <el-form-item label="排序权重" prop="personRank">
          <el-input-number v-model="temp.personRank" :min="0" :step="1" step-strictly style="text-align: center" size="medium" /><span style="color: gray; margin-left: 20px"> (权重数字越大，展示越靠前)</span>
        </el-form-item>
        <el-form-item label="详情介绍" prop="personContent" style="width: 90%">
          <tinymce v-if="dialogVisible" :id="'id'+temp.personId" v-model="temp.personContent" :height="180" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置排序权重" :visible.sync="dialogRankVisible" width="225px">
      <el-input-number v-model="tempRank.personRank" :min="0" :step="1" step-strictly style="text-align: center" />
      <p style="color: gray">(权重数字越大，展示越靠前)</p>
      <p style="color: gray"></p>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogRankVisible = false">
          取消
        </el-button>
        <el-button size="small" type="primary" @click="setRank()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateRank, deleteExcelPerson, saveOrUpdateExcelPerson, showExcelPersonList } from '@/api/excellent'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import ImgUploadSingle from '@/components/ImgUpload/ImgUploadSingle'
import Tinymce from '@/components/Tinymce'

export default {
  name: 'ExcellentPerson',
  components: { Pagination, ImgUploadSingle, Tinymce },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        personName: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      imgUploadKey: '',
      temp: {
        personId: undefined,
        personName: '',
        personImg: '',
        personDesc: '',
        personRank: '',
        personContent: ''
      },
      tempRank: {
        personId: undefined,
        personRank: 0
      },
      dialogVisible: false,
      dialogRankVisible: false,
      rules: {
        personName: [{ required: true, message: '请填写优秀员工姓名', trigger: 'change' }],
        personImg: [{ required: true, message: '请上传员工展示照片', trigger: 'change' }],
        personDesc: [{ required: true, message: '请填写所属单位', trigger: 'change' }],
        personRank: [{ required: true, message: '请设置排序权重', trigger: 'change' }]
      }
    }
  },
  created() {
    this.showExcelPersonList()
  },
  methods: {
    showExcelPersonList() {
      this.listLoading = true
      showExcelPersonList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showExcelPersonList()
    },
    resetDialog() {
      this.temp = {
        personId: undefined,
        personName: '',
        personImg: '',
        personDesc: '',
        personRank: '',
        personContent: ''
      }
      this.imgUploadKey = new Date().getTime()
    },
    handleCreate() {
      this.resetDialog()
      this.dialogStatus = 'create'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateExcelPerson(this.temp).then(() => {
            this.showExcelPersonList()
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              message: '新增版块成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.resetDialog()
      this.imgUploadKey = row.personId
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateExcelPerson(this.temp).then(() => {
            this.showExcelPersonList()
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              message: '编辑版块成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    resetRankDialog() {
      this.tempRank = {
        personId: undefined,
        personRank: 0
      }
    },
    handleRank(row) {
      this.resetRankDialog()
      this.tempRank.personId = row.personId
      this.tempRank.personRank = row.personRank
      this.dialogRankVisible = true
    },
    setRank() {
      updateRank(this.tempRank).then(() => {
        this.showExcelPersonList()
        this.dialogRankVisible = false
        this.$notify({
          title: 'Success',
          message: '设置排序权重成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(personId) {
      this.$confirm('此操作将不再展示该优秀员工，且不可逆，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteExcelPerson(personId).then(() => {
          this.showExcelPersonList()
          this.dialogVisible = false
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    removeImg() {
      this.temp.personImg = ''
    },
    uploadImg() {
      this.temp.personImg = this.$refs.imgUpload.url
    }
  }
}
</script>
